﻿using Microsoft.EntityFrameworkCore;
using SovaDataService.SoModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SovaDataService.SoDataServices
{
    public class AuthorDataService
    {

        /**
        * Retrieves an author for a given id.
        */
        public Author GetOne(int id)
        {
            using (var db = new DbContext())
            {
                IQueryable<Author> author = db.Authors
                    .Where(a => a.Id == id);
                return author.SingleOrDefault();
            }
        }

        /**
         * Retrieves all authors from the database.
         */
        public IEnumerable<Author> GetAll()
        {
            using (var db = new DbContext())
            {
                IQueryable<Author> authors = db.Authors
                    .OrderBy(au => au.Id)
                    .Include(au => au.Location);
                return authors.ToList();
            }
        }

        /**
         * Retrieves the total without having to fetch all the authors from db.
         */
        public int GetCount()
        {
            using (var db = new DbContext())
            {
                return db.Authors.Count();
            }
        }
    }
}
